﻿Imports DTO
Imports System.Data.OleDb
Public Class LoaiTietKiemDAO
    Public Shared Function LayLoaiSoTietKiem() As List(Of LoaiTietKiemDTO)
        Dim sql As String = "Select * From LOAITIETKIEM Where MaTinhTrangLoaiTietKiem=1"
        Dim conn As OleDbConnection = DataProvider.ConnectDB()
        Dim cmd As New OleDbCommand(sql, conn)
        Dim reader As OleDbDataReader = cmd.ExecuteReader()
        Dim danhsach As New List(Of LoaiTietKiemDTO)
        Dim temp As New LoaiTietKiemDTO()
        temp.MaLoaiTietKiem = 0
        temp.TenLoaiTietKiem = "--Chọn loại tiết kiệm--"
        temp.LaiSuat = 0.0
        temp.MaTinhTrangLoaiTietKiem = 1
        danhsach.Add(temp)
        While (reader.Read())
            Dim ltkDTO As New LoaiTietKiemDTO()
            ltkDTO.MaLoaiTietKiem = reader.GetInt32("0")
            ltkDTO.TenLoaiTietKiem = reader.GetString("1")
            ltkDTO.LaiSuat = reader.GetDouble("2")
            ltkDTO.MaTinhTrangLoaiTietKiem = reader.GetInt32("3")
            danhsach.Add(ltkDTO)
        End While
        
        conn.Close()
        Return danhsach
    End Function

    Public Function LayMaLoaiTietKiemMax() As Integer
        Dim maloai As Integer
        Dim cnn As New OleDbConnection()
        cnn = DataProvider.ConnectDB()
        Dim sql As String = "Select Max(MaLoaiTietKiem) From LoaiTietKiem"
        Dim comand As New OleDbCommand(sql, cnn)
        Try
            maloai = comand.ExecuteScalar()
        Catch ex As Exception
            maloai = 0
        End Try
        cnn.Close()
        Return maloai
    End Function

    Public Function ThemLoaiTietKiem(ByVal ltkDTO As LoaiTietKiemDTO) As Integer
        Dim cnn As OleDbConnection
        cnn = DataProvider.ConnectDB()
        Dim strSQL As String = "INSERT INTO LOAITIETKIEM(TenLoaiTietKiem,LaiSuat,MaTinhTrangLoaiTietKiem)VALUES(?,?,?)"
        Dim comand As New OleDbCommand(strSQL, cnn)
        comand.Parameters.Add("@TenLoaiTietKiem", OleDbType.WChar)
        comand.Parameters.Add("@LaiSuat", OleDbType.Single)
        comand.Parameters.Add("@MaTinhTrangLoaiTietKiem", OleDbType.Integer)

        comand.Parameters("@TenLoaiTietKiem").Value = ltkDTO.TenLoaiTietKiem
        comand.Parameters("@LaiSuat").Value = ltkDTO.LaiSuat
        comand.Parameters("@MaTinhTrangLoaiTietKiem").Value = 1
        Dim kq As Integer = comand.ExecuteNonQuery()
        ',@LaiSuat,@MaTinhTrangLoaiTietKiem")
        cnn.Close()
        Return kq
    End Function
End Class
